import { Component, OnInit, Input } from '@angular/core';
import { APIBase } from 'app/modules/base/api-base';
import { BehaviorSubject } from 'rxjs';
import { SweetAlertService } from 'app/modules/share/services/sweet-alert.service';

@Component({
	selector: 'm-api-list',
	templateUrl: './api-list.component.html',
	styleUrls: ['./api-list.component.scss'],
})
export class ApiListComponent implements OnInit {
	constructor(private sweet: SweetAlertService) {}
	@Input() API: APIBase;
	list$ = new BehaviorSubject([]);
	add = false;
	ngOnInit() {
		this.getData();
	}
	async getData() {
		const result = await this.API.list();
		this.list$.next(result.list);
	}
	async save(name: string) {
		await this.API.create({ name });
		this.add = false;
		this.getData();
	}
	async delete(id: number) {
		const result = await this.sweet.confirm('确定要删除吗？');
		if (result.dismiss) {
			return;
		}
		await this.API.delete([id]);
		this.getData();
	}
}
